﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="RequestContextTests.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagementTests
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using RestClient;

    using Xunit;

    public class RequestContextTests
    {
        [Fact]
        public void CanAddParametersWhen1Param()
        {
            var requestContext = new RequestContext();
            requestContext.AddParameter("param1", "value1");

            Assert.Equal("?param1=value1", requestContext.GetQueryString());
        }

        [Fact]
        public void CanAddParametersAndPathWhen1Param()
        {
            const string NewPath = "test/path";

            var requestContext = new RequestContext { Path = NewPath };
            requestContext.AddParameter("param1", "value1");

            Assert.Equal("?param1=value1", requestContext.GetQueryString());
            Assert.Equal(NewPath, requestContext.Path);
        }

        [Fact]
        public void CanAddParametersWhenMultipleParams()
        {
            const int ParamCount = 10;
            var parameters = new Dictionary<string, string>();
            for (var i = 0; i < ParamCount; i++)
            {
                parameters.Add(string.Format("param{0}", i), string.Format("value{0}", i));
            }

            var requestContext = new RequestContext();
            foreach (var keyValuePair in parameters)
            {
                requestContext.AddParameter(keyValuePair.Key, keyValuePair.Value);
            }

            var stringBuilder = new StringBuilder();
            foreach (var keyValuePair in parameters)
            {
                if (parameters.Keys.First() == keyValuePair.Key)
                {
                    stringBuilder.AppendFormat("?{0}={1}", keyValuePair.Key, keyValuePair.Value);
                    continue;
                }

                stringBuilder.AppendFormat("&{0}={1}", keyValuePair.Key, keyValuePair.Value);
            }

            Assert.Equal(stringBuilder.ToString(), requestContext.GetQueryString());
        }

        [Fact]
        public void CanAddParametersAndPathWhenMultipleParams()
        {
            const int ParamCount = 10;
            const string NewPath = "test/path";

            var parameters = new Dictionary<string, string>();
            for (int i = 0; i < ParamCount; i++)
            {
                parameters.Add(string.Format("param{0}", i), string.Format("value{0}", i));
            }

            var requestContext = new RequestContext { Path = NewPath };
            foreach (var keyValuePair in parameters)
            {
                requestContext.AddParameter(keyValuePair.Key, keyValuePair.Value);
            }

            var stringBuilder = new StringBuilder();
            foreach (var keyValuePair in parameters)
            {
                if (parameters.Keys.First() == keyValuePair.Key)
                {
                    stringBuilder.AppendFormat("?{0}={1}", keyValuePair.Key, keyValuePair.Value);
                    continue;
                }

                stringBuilder.AppendFormat("&{0}={1}", keyValuePair.Key, keyValuePair.Value);
            }

            Assert.Equal(stringBuilder.ToString(), requestContext.GetQueryString());
        }
    }
}